﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

using AttributeRouting;
using AttributeRouting.Web.Mvc;
using Newtonsoft.Json.Linq;

using PH.Model.Results;
using PH.Model.Standards;
using PH.Logic.Standards;
namespace PH.Web.Management.Areas.Standard.Controllers
{
    [RouteArea("Standard")]
    [RoutePrefix("Subject")]
    public class SubjectApiController : ApiController
    {
        [Route("GetSubjects")]
        [HttpPost]
        public JsonEntitySet<SubjectModel> GetSubjects(JObject request)
        {
            string errinfo = "no data";
            try
            {
                SubjectQuery search = new SubjectQuery();
                search.PageIndex = request.Value<int>("pageIndex");
                search.PageSize = request.Value<int>("pageSize");
                search.Key = request.Value<string>("key");
                search.GradeCode = request.Value<int>("gradeCode");
                var cat = request.Value<string>("category");
                if (!string.IsNullOrEmpty(cat))
                    search.Category = cat.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(p => byte.Parse(p)).ToArray();
                var result = SubjectLogic.GetSubjects(search);
                return new JsonEntitySet<SubjectModel>(result);
            }
            catch (Exception ex)
            {
                errinfo = ex.Message;
                Common.Logger.Error("", ex);
            }
            return new JsonEntitySet<SubjectModel>(null, errinfo, false);
        }

        [Route("SaveSubject")]
        [HttpPost]
        public JsonEntity<SubjectModel> SaveSubject(JObject request)
        {
            string errinfo = "对不起！保存失败，出错原因：";
            try
            {
                #region 收集参数
                SubjectModel sub = new SubjectModel();
                sub.ID = request.Value<int>("id");
                sub.SubjectName = request.Value<string>("subjectName");
                sub.Unit = request.Value<string>("unit");
                #endregion

                if (sub.ID == 0)
                    sub = SubjectLogic.CreateNew(sub);
                else
                    SubjectLogic.Update(sub);
                return new JsonEntity<SubjectModel>(sub);
            }
            catch (Exception ex)
            {
                errinfo += ex.Message;
                Common.Logger.Error("", ex);
            }
            return new JsonEntity<SubjectModel>(null, errinfo, false);
        }

        [Route("DeleteSubject")]
        [HttpPost]
        public JsonResult<bool> DeleteSubject(JObject request)
        {
            string errinfo = "对不起！删除失败，出错原因：";
            try
            {
                var sid = request.Value<int>("id");
                SubjectLogic.Delete(sid);
                return new JsonResult<bool>(true);
            }
            catch (Exception ex)
            {
                errinfo += ex.Message;
                Common.Logger.Error("", ex);
            }
            return new JsonResult<bool>(false, errinfo, false);
        }
    }
}
